<?php

class DownloadApp extends MemberbaseApp
{
	var $_upload_mod; 
    function __construct()
    {
        $this->DownloadApp();
    }
    function DownloadApp()
    {
        parent::__construct();
		$this->_upload_mod =& m('upload');
    }
	
    function index()
    {
		$id = empty($_GET['id']) ? 0 : intval($_GET['id']);
		if (!$id) {
			$this->show_warning('file_not_exist');
			return;
		}
		$file = $this->_upload_mod->get_info($id);
		$file_name = $file['file_name']; 
		$file_path = $file['file_path'];
		if (!file_exists($file_path)) {
			$this->show_warning('file_not_exist');
			return; 
		} else { 
			header("Content-type: application/octet-stream; charset=utf-8"); 
			header("Accept-Ranges: bytes"); 
			header("Accept-Length: " . filesize($file_path)); 
			header("Content-Length: " .filesize($file_path));
			if(strpos($_SERVER["HTTP_USER_AGENT"],"MSIE")) {
				header("Content-Disposition: attachment; filename=\"" . rawurlencode($file_name) . '"'); 
			} else {
				header("Content-Disposition: attachment; filename=\"" . $file_name . '"'); 
			}
			/*$download = fopen($file_path, "rb"); 
			echo fread($download, filesize($file_path));
			fclose($download);
			return;*/
		} 
    }

    function _return_mimetype($filename)
    {
        preg_match("|\.([a-z0-9]{2,4})$|i", $filename, $fileSuffix);
        switch(strtolower($fileSuffix[1]))
        {
            case "js" :
                return "application/x-javascript";

            case "json" :
                return "application/json";

            case "jpg" :
            case "jpeg" :
            case "jpe" :
                return "image/jpeg";

            case "png" :
            case "gif" :
            case "bmp" :
            case "tiff" :
                return "image/".strtolower($fileSuffix[1]);

            case "css" :
                return "text/css";

            case "xml" :
                return "application/xml";

            case "doc" :
            case "docx" :
                return "application/msword";

            case "xls" :
            case "xlsx" :
            case "xlt" :
            case "xlm" :
            case "xld" :
            case "xla" :
            case "xlc" :
            case "xlw" :
            case "xll" :
                return "application/vnd.ms-excel";

            case "ppt" :
            case "pps" :
                return "application/vnd.ms-powerpoint";

            case "rtf" :
                return "application/rtf";

            case "pdf" :
                return "application/pdf";

            case "html" :
            case "htm" :
            case "php" :
                return "text/html";

            case "txt" :
                return "text/plain";

            case "mpeg" :
            case "mpg" :
            case "mpe" :
                return "video/mpeg";

            case "mp3" :
                return "audio/mpeg3";

            case "wav" :
                return "audio/wav";

            case "aiff" :
            case "aif" :
                return "audio/aiff";

            case "avi" :
                return "video/msvideo";

            case "wmv" :
                return "video/x-ms-wmv";

            case "mov" :
                return "video/quicktime";

            case "rar" :
                return "application/x-rar-compressed";

            case "zip" :
            return "application/zip";

            case "tar" :
                return "application/x-tar";

            case "swf" :
                return "application/x-shockwave-flash";

            default :
            /*if(function_exists("mime_content_type"))
            {
                $fileSuffix = mime_content_type($filename);
            }*/
            	return "application/octet-stream";
        }
    }
}

?>
